package com.atguigu.a_set;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class Demo {
    public static void main(String[] args) {

        //创建Set集合
        Set<String> set = new HashSet<>();
        System.out.println("set = " + set);//[]

        //添加元素
        set.add("张三");
        set.add("李四");
        set.add("王五");


        //存取无序
        System.out.println("set = " + set);

        //遍历
        Object[] objs = set.toArray();
        for (int i = 0; i < objs.length; i++) {
            String name = (String) objs[i];
            System.out.println("name = " + name);
        }
        System.out.println("------------------------");
        //迭代器
        Iterator<String> it = set.iterator();
        while(it.hasNext()){
            //获取下一个元素
            String name = it.next();
            System.out.println("name = " + name);
        }
        System.out.println("------------------------");
        //增强for
        for (String name : set) {
            System.out.println("name = " + name);
        }
    }
}
